﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GBPVR.Public;
using System.Drawing;
using System.Collections;
using GBPVRLibrary.UtilityLibrary;

namespace GBPVRLibrary.ControlLibrary
{
    public class ProgressBar:GuiElement
    {
        private GBPVRUiElement ProgressBarBackground, ProgressBarBar, ProgressBarOverlay;
        private float _Value = 0;
        private RectangleF ProgressBarRect, ProgressBarBackgroundRect;

        public ProgressBar(RectangleF Location)
        {
            this.Location = Location;
            ProgressBarBar = new GBPVRUiElement("ProgressBar-Bar", new RectangleF(), GuiElement.SkinHelper.getNamedImage("ProgressBar-Bar", new Hashtable()));
            ProgressBarRect = GuiElement.SkinHelper.getNamedImageRect("ProgressBar-Bar");
            if (GuiElement.SkinHelper.checkCompositeImageDefined("ProgressBar-Background"))
            {
                ProgressBarBackground = new GBPVRUiElement("ProgressBar-Background", Location, GuiElement.SkinHelper.getNamedImage("ProgressBar-Background", new Hashtable()));
                ProgressBarBackgroundRect = GuiElement.SkinHelper.getNamedImageRect("ProgressBar-Background");
                ProgressBarBackground.SetRectangle(new RectangleF(this.Location.X + (this.Location.X * ProgressBarBackgroundRect.X / 100),
                                                             this.Location.Y + (this.Location.Y * ProgressBarBackgroundRect.Y / 100),
                                                             this.Location.Width * ProgressBarBackgroundRect.Width / 100,
                                                             this.Location.Height * ProgressBarBackgroundRect.Height / 100));
            }
            if (GuiElement.SkinHelper.checkCompositeImageDefined("ProgressBar-Overlay"))
                ProgressBarBackground = new GBPVRUiElement("ProgressBar-Overlay", Location, GuiElement.SkinHelper.getNamedImage("ProgressBar-Overlay", new Hashtable()));
            
        }

        protected override void Render(System.Collections.ArrayList RenderList, ref bool RequiresMoreRendering)
        {
            if (ProgressBarBackground != null)
                RenderList.Add(ProgressBarBackground);
            RenderList.Add(ProgressBarBar);
            if(ProgressBarOverlay != null)
                RenderList.Add(ProgressBarOverlay);
        }

        public float Value
        {
            get { return _Value; }
            set
            {
                if (value < 0)
                    _Value = 0;
                else if (value > 1)
                    _Value = 1;
                else
                    _Value = value;
                ProgressBarBar.SetRectangle(new RectangleF(this.Location.X + (ProgressBarRect.X * this.Location.X / 100),
                                                      this.Location.Y + (ProgressBarRect.Y * this.Location.Y / 100),
                                                      this.ProgressBarRect.Width * _Value * this.Location.Width / 100, this.ProgressBarRect.Height * this.Location.Height / 100));
            }
        }
    }
}
